package com.ahm.controller;


import com.ahm.base.BaseController;
import com.ahm.base.ResultInfo;
import com.ahm.bean.Role;
import com.ahm.dto.TreeNode;
import com.ahm.query.RoleQuery;
import com.ahm.service.RoleService;
import com.ahm.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController  extends BaseController {


    @Autowired(required = false)
    private RoleService roleService;



    @RequestMapping("roles")
    @ResponseBody
    public List<Map<String,Object>> sayRoles(Integer userId){
       return roleService.findRoles(userId);
    }


    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> findRoleByParam(RoleQuery query){
        //根据条件查询角色
        Map<String, Object> map = roleService.findAllRole(query);
        //map--json
        return map;
    }


    @RequestMapping("index")
    public String index(){
        return "role/role";
    }


//    /role/addOrUpdate
    @RequestMapping("addOrUpdate")
    public String addOrUpdate(Integer roleId, Model model){
        if (roleId!=null){
            // 查询对应的角色
            Role role = roleService.selectByPrimaryKey(roleId);
            model.addAttribute("role",role);
        }
        return "role/add_update";
    }


    @RequestMapping("save")
    @ResponseBody
    public ResultInfo save(Role role){
        roleService.insertRole(role);
        return success("添加成功");
    }

    @RequestMapping("update")
    @ResponseBody
    public ResultInfo update(Role role){

        roleService.updateRole(role);
        return success("修改成功");

    }
    /*
    * 删除角色
    * */
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo delete(Integer id){
        AssertUtil.isTrue(id==null || id==0,"请选中要删除的记录");
        AssertUtil.isTrue(roleService.deleteByPrimaryKey(id)<1,"删除失败");
        return success("删除成功");
    }

//
    @RequestMapping("addGrant")
    @ResponseBody
    public ResultInfo addGrant(Integer[] mids,Integer roleId){
//mids[]  roleId
        roleService.addGrant(mids,roleId);
        return success("授权成功");

    }

}
